/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sermonuploader;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;

/**
 *
 * @author Graeme
 */
public class Uploader extends Thread{

    String fileName;
    File file;
    JProgressBar progbar;
    String db_url;
    String db_username;
    String db_password;
    String ftp_url;
    String ftp_username;
    String ftp_password;
    String ftp_path;
           
    public Uploader(String filename, File file, JProgressBar progbar)
    {
        try {
            this.fileName=filename;
            this.file=file;
            this.progbar=progbar;
            Properties configFile = new Properties();
            ClassLoader cl = this.getClass().getClassLoader();
            InputStream is = cl.getResourceAsStream("./sermonuploader/resources/sermonUploader.properties");
            configFile.load(is);
            db_url = configFile.getProperty("DB_URL");
            db_username = configFile.getProperty("DB_USERNAME");
            db_password = configFile.getProperty("DB_PASSWORD");
            ftp_url = configFile.getProperty("FTP_SERVER");
            ftp_username = configFile.getProperty("FTP_USERNAME");
            ftp_password = configFile.getProperty("FTP_PASSWORD");
            ftp_path = configFile.getProperty("FTP_PATH");

        } catch (IOException ex) {
            Logger.getLogger(Uploader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run()
    {
        //String ftpServer="98.131.48.2";
        String ftpServer=ftp_url;
        //String user="comprod";
        String user=ftp_username;
        //String password="H04jibFsBkub";
        String password=ftp_password;
        File source = file;

        if (ftpServer != null && fileName != null && source != null)
      {
         StringBuffer sb = new StringBuffer( "ftp://" );
         // check for authentication else assume its anonymous access.
         if (user != null && password != null)
         {
            sb.append( user );
            sb.append( ':' );
            sb.append( password );
            sb.append( '@' );
         }
         sb.append( ftpServer );
         sb.append( ftp_path );
         sb.append( fileName );
         /*
          * type ==> a=ASCII mode, i=image (binary) mode, d= file directory
          * listing
          */
         sb.append( ";type=i" );

         BufferedInputStream bis = null;
         BufferedOutputStream bos = null;
         try
         {
            URL url = new URL( sb.toString() );
            URLConnection urlc = url.openConnection();

            bos = new BufferedOutputStream( urlc.getOutputStream() );
            bis = new BufferedInputStream( new FileInputStream( source ) );


            int i;
            // read byte by byte until end of stream
            progbar.setMaximum((int) file.length());
            int count=0;
            while ((i = bis.read()) != -1)
            {
               count++;
               progbar.setValue(count);
               progbar.repaint();
               bos.write( i );
            }
         }
            catch (IOException ex) {
                Logger.getLogger(Uploader.class.getName()).log(Level.SEVERE, null, ex);
            }         finally
         {
            if (bis != null)
               try
               {
                  bis.close();

               }
               catch (IOException ioe)
               {

                  ioe.printStackTrace();
               }
            if (bos != null)
               try
               {
                   JOptionPane.showMessageDialog(null,"File uploaded successfully as "+fileName,"Upload successful", JOptionPane.INFORMATION_MESSAGE);
                   progbar.setValue(0);
                  bos.close();

               }
               catch (IOException ioe)
               {
                  JOptionPane.showMessageDialog(null,"File upload failed. Please try again.","Upload failed", JOptionPane.ERROR_MESSAGE);
                  ioe.printStackTrace();
               }
         }
      }
      else
      {
         JOptionPane.showMessageDialog(null,"File upload failed. Please try again.","Upload failed", JOptionPane.ERROR_MESSAGE);
         System.out.println( "Input not available." );
      }


    }

}
